var exportExam = new Object();

exportExam.init = function () {

    exportExam.initTable();
}


exportExam.initTable = function () {
    var num = ['一','二','三','四','五','六','七','八'];

    var questionTypeNum = 0;

    var url = appPath + '/admin/getProjectPaper';
    var param = {"examNo" : $("#examNo").val(), "projectId" : $("#projectId").val(),"roleId" : $("#roleId").val()};
    var data = exportExam.ajax(url, param);
    var questions = data['result'].dataList;
    var html_single = '',html_many = '',html_judge = '',html_fillout='',html_quesAns='',html_discuss='',html_analyse='',html_drawing='';
    if(questions.length < 1){
        html = '<div><div class="no-content"></div></div>';
    }
    // 试题编号
    var x=1,y=1,z=1,f=1,q=1,d=1,a=1,r=1;
    // 答案集合
    var singleAnswerList=[],manyAnswerList=[],judgeAnswerList=[], filloutAnswerList=[],quesAnsAnswerList=[],discussAnswerList=[],analyseAnswerList=[],drawingAnswerList=[];
    var obj;
    // 试题分类
    for(var i=0; i<questions.length; i++){

        obj = new Object();
        var question = questions[i];
        var chrType = question.chrType;
        if(chrType == '01'){
            html_single += exportExam.single_html(question, i, x);
            obj.num = x;
            obj.answer = question.varAnswer;
            singleAnswerList.push(obj);
            x++;
        }else if(chrType == '02'){
            html_many += exportExam.many_html(question, i, y);
            obj.num = y;
            obj.answer = question.varAnswer;
            manyAnswerList.push(obj);
            y++;
        }else if(chrType == '03'){
            html_judge += exportExam.judge_html(question, i, z);
            obj.num = z;
            obj.answer = question.varAnswer;
            judgeAnswerList.push(obj);
            z++;
        }else if(chrType == '04'){
            html_fillout += exportExam.fillout_html(question, i, f);
            obj.num = f;
            obj.answer = question.varAnswerDesc;
            filloutAnswerList.push(obj);
            f++;
        }else if(chrType == '05'){
            html_quesAns += exportExam.quesAns_html(question, i, q);
            obj.num = q;
            obj.answer = question.varAnswerDesc;
            quesAnsAnswerList.push(obj);
            q++;
        }else if(chrType == '06'){
            html_discuss += exportExam.discuss_html(question, i, d);
            obj.num = d;
            obj.answer = question.varAnswerDesc;
            discussAnswerList.push(obj);
            d++;
        }else if(chrType == '07'){
            html_analyse += exportExam.analyse_html(question, i, a);
            obj.num = a;
            obj.answer = question.varAnswerDesc;
            analyseAnswerList.push(obj);
            a++;
        }else if(chrType == '08'){
            html_drawing += exportExam.drawing_html(question, i, r);
            obj.num = r;
            obj.answer = question.varAnswerDesc;
            drawingAnswerList.push(obj);
            r++;
        }
    }
    // 渲染试卷
    $(".score").html(data['result'].totalScore);

    //单选
    if(x > 1){

        $(".single").show();
        $("#singleQuestion").append(html_single);
        $(".single_total").html(x-1);
        $(".single_score").html(data['result'].singleScore);
        questionTypeNum += 1;
        $(".single").find(".title_bg").html(num[questionTypeNum-1]+"、单项选择题");
    }
    //多选
    if(y > 1){
        $(".many").show();
        $("#manyQuestion").append(html_many);
        $(".many_total").html(y-1);
        $(".many_score").html(data['result'].manyScore);
        questionTypeNum += 1;
        $(".many").find(".title_bg").html(num[questionTypeNum-1]+"、多选题");
    }
    //判断
    if(z > 1){
        $(".judge").show();
        $("#judgeQuestion").append(html_judge);
        $(".judge_total").html(z-1);
        $(".judge_score").html(data['result'].judgeScore);
        questionTypeNum += 1;
        $(".judge").find(".title_bg").html(num[questionTypeNum-1]+"、判断题");
    }

    //主观题
    if(f > 1){
        $(".fillout").show();
        $("#filloutQuestion").append(html_fillout);
        $(".fillout_total").html(f-1);
        $(".fillout_score").html(data['result'].filloutScore);
        questionTypeNum += 1;
        $(".fillout").find(".title_bg").html(num[questionTypeNum-1]+"、填空题");
    }
    if(q > 1){
        $(".quesAns").show();
        $("#quesAnsQuestion").append(html_quesAns);
        $(".ques_ans_total").html(q-1);
        $(".ques_ans_score").html(data['result'].quesAnsScore);
        questionTypeNum += 1;
        $(".quesAns").find(".title_bg").html(num[questionTypeNum-1]+"、简答题");
    }
    if(d > 1){
        $(".discuss").show();
        $("#discussQuestion").append(html_discuss);
        $(".discuss_total").html(d-1);
        $(".discuss_score").html(data['result'].discussScore);
        questionTypeNum += 1;
        $(".discuss").find(".title_bg").html(num[questionTypeNum-1]+"、论述题");
    }
    if(a > 1){
        $(".analyse").show();
        $("#analyseQuestion").append(html_analyse);
        $(".analyse_total").html(a-1);
        $(".analyse_score").html(data['result'].analyseScore);
        questionTypeNum += 1;
        $(".analyse").find(".title_bg").html(num[questionTypeNum-1]+"、分析题");
    }
    if(r > 1){
        $(".drawing").show();
        $("#drawingQuestion").append(html_drawing);
        $(".drawing_total").html(r-1);
        $(".drawing_score").html(data['result'].drawScore);
        questionTypeNum += 1;
        $(".drawing").find(".title_bg").html(num[questionTypeNum-1]+"、绘图题");
    }

    // 渲染参考答案
    var html_single_answer = '', html_many_answer = '', html_judge_answer = '', html_fillout_answer='', html_quesAns_answer='',html_discuss_answer='',html_analyse_answer='',html_drawing_answer='';
    html_single_answer += exportExam.single_answer_html(singleAnswerList);
    $("#singleQuestionAnswer").append(html_single_answer);
    html_many_answer += exportExam.many_html_answer(manyAnswerList);
    $("#manyQuestionAnswer").append(html_many_answer);
    html_judge_answer = exportExam.judge_answer_html(judgeAnswerList);
    $("#judgeQuestionAnswer").append(html_judge_answer);

    //主观题答案
    html_fillout_answer = exportExam.judge_answer_fillout(filloutAnswerList);
    $("#filloutQuestionAnswer").append(html_fillout_answer);
    html_quesAns_answer = exportExam.judge_answer_quesAns(quesAnsAnswerList);
    $("#quesAnsQuestionAnswer").append(html_quesAns_answer);
    html_discuss_answer = exportExam.judge_answer_discuss(discussAnswerList);
    $("#discussQuestionAnswer").append(html_discuss_answer);
    html_analyse_answer = exportExam.judge_answer_analyse(analyseAnswerList);
    $("#analyseQuestionAnswer").append(html_analyse_answer);
    html_drawing_answer = exportExam.judge_answer_draw(drawingAnswerList);
    $("#drawingQuestionAnswer").append(html_drawing_answer);
}

// 单选题
exportExam.single_html = function (question, i, x) {
    var html = '';
    html += '<div id="div' + (i + 1) + '" class="options"><div>';
    html += '<div class="questions"><div><p>'+ x +'、'+ question.varQuestionsContent+'</p></div>';
    html += '<div class="show_img clearfix">';
    var questionTitleFiles = question.questionTitleFiles;
    if(questionTitleFiles != undefined){
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img width="500" src="' + file.mimeUrl + '" alt="">';
            }
        }
    }
    html += ' </div>';
    var options = question.options;
    if(options != undefined){
        for(var i=0; i<options.length; i++){
            var q = options[i];
            html += '<div class="option" data-type="' + q.oType + '">';
            html += '<span></span>';
            html += '<div><p>'+q.oType + '.&nbsp' + q.optionContent+'</p></div>';
            html += '<div class="show_img clearfix">';
            if(q.mimeType == 'img'){
                html += '<img width="200" src="' + q.mimeUrl + '" alt="">';
            }
            html += '</div>';
            html += '</div>';

        }
    }
    html += '</div></div></div>';
    return html;
}

// 多选题
exportExam.many_html = function (question, i, y) {
    var html = '';
    html += '<div id="div' + (i + 1) + '" class="options"><div>';
    html += '<div class="questions"><div><p>'+ y +'、 '+ question.varQuestionsContent+'</p></div>';
    html += '<div class="show_img clearfix">';
    var questionTitleFiles = question.questionTitleFiles;
    if(questionTitleFiles != undefined){
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img width="500" src="' + file.mimeUrl + '" alt="">';
            }
        }
    }
    html += ' </div>';
    var options = question.options;
    if(options != undefined){
        for(var i=0; i<options.length; i++){
            var q = options[i];
            html += '<div class="option" data-type="' + q.oType + '">';
            html += '<span></span>';
            html += '<div><p>'+q.oType + '. &nbsp' + q.optionContent+'</p></div>';
            html += '<div class="show_img clearfix">';
            if(q.mimeType == 'img'){
                html += '<img width="200" src="' + q.mimeUrl + '" alt="">';
            }
            html += '</div>';
            html += '</div>';
        }
    }
    html += '</div></div></div>';
    return html;
}

// 判断题
exportExam.judge_html = function (question, i, z) {
    var html = '';
    html += '<div id="div' + (i + 1) + '" class="options"><div>';
    html += '<div class="questions"><div><p>'+ z +'、'+ question.varQuestionsContent+'</p></div>';
    html += '<div class="show_img clearfix">';
    var questionTitleFiles = question.questionTitleFiles;
    if(questionTitleFiles != undefined){
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img width="500" src="' + file.mimeUrl + '" alt="">';
            }
        }
    }
    html += ' </div></div>';
    html += '<div>';
    html += '<div class="option" data-type="A">';
    html += '<span></span>';
    html += '<div>A、正确</div>';
    html += '</div>';
    html += '</div>';
    html += '<div>';
    html += '<div class="option" data-type="B">';
    html += '<span></span>';
    html += '<div>B、错误</div>';
    html += '</div></div></div></div>';
    return html;
}


//填空题
exportExam.fillout_html = function (question, i, f) {
    var html = '';
    html += '<div id="div' + (i + 1) + '" class="options"><div>';
    html += '<div class="questions"><div><p>'+ f +'、'+ question.varQuestionsContent+'</p></div>';
    html += '<div class="show_img clearfix">';
    var questionTitleFiles = question.questionTitleFiles;
    if(questionTitleFiles != undefined){
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img width="500" src="' + file.mimeUrl + '" alt="">';
            }
        }
    }
    html += ' </div></div></div></div>';
    return html;
}

//问答题
exportExam.quesAns_html = function(question, i, q){
    var html = '';
    html += '<div id="div' + (i + 1) + '" class="options"><div>';
    html += '<div class="questions"><div><p>'+ q +'、'+ question.varQuestionsContent+'</p></div>';
    html += '<div class="show_img clearfix">';
    var questionTitleFiles = question.questionTitleFiles;
    if(questionTitleFiles != undefined){
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img width="500" src="' + file.mimeUrl + '" alt="">';
            }
        }
    }
    html += ' </div></div></div></div>';
    return html;
}

// 论述题
exportExam.discuss_html = function(question, i, d){
    var html = '';
    html += '<div id="div' + (i + 1) + '" class="options"><div>';
    html += '<div class="questions"><div><p>'+ d +'、'+ question.varQuestionsContent+'</p></div>';
    html += '<div class="show_img clearfix">';
    var questionTitleFiles = question.questionTitleFiles;
    if(questionTitleFiles != undefined){
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img width="500" src="' + file.mimeUrl + '" alt="">';
            }
        }
    }
    html += ' </div></div></div></div>';
    return html;
}

// 分析题
exportExam.analyse_html = function(question, i, a){
    var html = '';
    html += '<div id="div' + (i + 1) + '" class="options"><div>';
    html += '<div class="questions"><div><p>'+ a +'、'+ question.varQuestionsContent+'</p></div>';
    html += '<div class="show_img clearfix">';
    var questionTitleFiles = question.questionTitleFiles;
    if(questionTitleFiles != undefined){
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img width="500" src="' + file.mimeUrl + '" alt="">';
            }
        }
    }
    html += ' </div></div></div></div>';
    return html;
}

// 绘图题
exportExam.drawing_html = function(question, i, r){
    var html = '';
    html += '<div id="div' + (i + 1) + '" class="options"><div>';
    html += '<div class="questions"><div><p>'+ r +'、'+ question.varQuestionsContent+'</p></div>';
    html += '<div class="show_img clearfix">';
    var questionTitleFiles = question.questionTitleFiles;
    if(questionTitleFiles != undefined){
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img width="500" src="' + file.mimeUrl + '" src="'+ file.mimeUrl +'" alt="">';
            }
        }
    }
    html += ' </div></div></div></div>';
    return html;
}

// 单选参考答案
exportExam.single_answer_html = function(singleAnswerList){
    var html = '';
    for (var i=1; i<=singleAnswerList.length; i++) {
        html += '<span>'+singleAnswerList[i-1].num+'.'+singleAnswerList[i-1].answer+'</span>';
        if(i%10==0){
            html += '<br>'
        }
    }
    return html;
}
// 多选参考答案
exportExam.many_html_answer = function(manyAnswerList){
    var html = '';
    for (var i=1; i<=manyAnswerList.length; i++) {
        html += '<span>'+manyAnswerList[i-1].num+'.'+manyAnswerList[i-1].answer+'</span>';
        if(i%10==0){
            html += '<br>'
        }
    }
    return html;
}
// 判断参考答案
exportExam.judge_answer_html = function(judgeAnswerList){
    var html = '';
    for (var i=1; i<=judgeAnswerList.length; i++) {
        html += '<span>'+judgeAnswerList[i-1].num+'.'+judgeAnswerList[i-1].answer+'</span>';
        if(i%10==0){
            html += '<br>'
        }
    }
    return html;
}
//填空题答案
exportExam.judge_answer_fillout = function(filloutAnswerList){
    var html = '';
    var answer ;
    var num = 0;
    var content = '';
    for (var i=1; i<=filloutAnswerList.length; i++) {
        // {"answers":[{"item":1,"itemDesc":"空一答案一|空一答案二|空一答案三"},{"item":2,"itemDesc":"空二答案"}]}
        answer = JSON.parse(filloutAnswerList[i-1].answer).answers;
        num = filloutAnswerList[i-1].num;
        content = '';
        for(var j=0;j<answer.length;j++){
            content += content == '' ? '{'+answer[j].itemDesc+'}' : ',{'+answer[j].itemDesc+'}'
        }
        html += '<span>'+num+'.'+content+'</span>';
        if(i%10==0){
            html += '<br>'
        }
    }
    return html;
}
//问答题答案
exportExam.judge_answer_quesAns = function(quesAnsAnswerList){
    var html = '';
    var answer ;
    var num = 0;
    var content = '';
    for (var i=1; i<=quesAnsAnswerList.length; i++) {
        // {"content":"小接地电流系统发生单相接地故障时，接地续供电。","images":[]}
        answer = JSON.parse(quesAnsAnswerList[i-1].answer);
        num = quesAnsAnswerList[i-1].num;
        content = answer.content;

        html += '<span>'+num+'.'+content+'</span>';
        if(i%10==0){
            html += '<br>'
        }
    }
    return html;
}
//论述题答案
exportExam.judge_answer_discuss = function(discussAnswerList){
    var html = '';
    var answer ;
    var num = 0;
    var content = '';
    for (var i=1; i<=discussAnswerList.length; i++) {

        answer = JSON.parse(discussAnswerList[i-1].answer);
        num = discussAnswerList[i-1].num;
        content = answer.content;

        html += '<span>'+num+'.'+content+'</span>';
        if(i%10==0){
            html += '<br>'
        }
    }
    return html;
}

//分析题答案
exportExam.judge_answer_analyse = function(analyseAnswerList){
    var html = '';
    var answer ;
    var num = 0;
    var content = '';
    for (var i=1; i<=analyseAnswerList.length; i++) {

        answer = JSON.parse(analyseAnswerList[i-1].answer);
        num = analyseAnswerList[i-1].num;
        content = answer.content;

        html += '<span>'+num+'.'+content+'</span>';
        if(i%10==0){
            html += '<br>'
        }
    }
    return html;
}

//绘图题答案
exportExam.judge_answer_draw = function(drawingAnswerList){
    var html = '';
    var answer ;
    var num = 0;
    var content = '';
    for (var i=1; i<=drawingAnswerList.length; i++) {

        answer = JSON.parse(drawingAnswerList[i-1].answer);
        num = drawingAnswerList[i-1].num;
        content = answer.content;

        html += '<span>'+num+'.'+content+'</span>';
        if(i%10==0){
            html += '<br>'
        }
    }
    return html;
}

// 获取数据
exportExam.ajax = function(url, param){
    var result = '';
    $.ajax({
        url: url,
        async: false,
        type: 'post',
        data: param,
        success: function(data){
            result = data;
        }
    });
    return result;
};

// 打印
exportExam.preview = function () {
    $(".sel_btn").hide();
    setTimeout(function(){
        $(".sel_btn").show();
    },3000);
    /*var bdhtml = window.document.body.innerHTML;
      var sprnstr = "";
      var eprinstr = "";
       var prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr)+18);
        prnhtml = prnhtml.substring(0,prnhtml.indexOf(eprinstr));
      window.document.body.innerHTML = prnhtml;*/
    window.print();
    //window.document.body.innerHTML = bdhtml;
}